package ua.kharkov.khpi.tarasenko.bach.core;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class LocalizedResourceManager {

	private static String resources;

	private ResourceBundle bundle;

	/**
	 * Creates new instance.
	 * 
	 * @param locale
	 *            to get resources for;
	 * @exception IllegalAccessError
	 *                if connection manager isn't initialized.
	 */
	public LocalizedResourceManager(Locale locale) {

		synchronized (resources) {

			if (!isInitialized()) {
				throw new IllegalAccessError(
						"Connection Manager isn't initialized!");
			}

			bundle = ResourceBundle.getBundle(resources, locale);
		}
	}

	/**
	 * Initialize LocalizedResourceManager.
	 * 
	 * @param res
	 *            - resources file name;
	 * @exception IllegalAccessError
	 *                if LocalizedResourceManager already initialized.
	 */
	public static void init(String res) {
		if (!isInitialized()) {
			resources = res;
		} else {
			throw new IllegalAccessError(
					"LocalizedResourceManager already initialized!");
		}
	}

	/**
	 * Gets the string for specified key.
	 * 
	 * @param key
	 *            - the key for string;
	 * @return message - the string for given key;
	 * 
	 * @exception NullPointerException
	 *                if <code>key</code> is <code>null</code>;
	 * @exception MissingResourceException
	 *                if no object for the given key can be found;
	 * @exception ClassCastException
	 *                if the object found for the given key is not a string.
	 * 
	 */
	public String getMessage(String key) {
		String message = bundle.getString(key);

		return message;
	}

	public static boolean isInitialized() {
		if ((resources == null) || (resources.isEmpty())) {
			return false;
		} else {
			return true;
		}
	}
}
